package com.kuanter.kuanterauto.adapter;

import java.util.ArrayList;
import java.util.List;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RatingBar;
import android.widget.TextView;

import com.kuanter.kuanterauto.R;
import com.kuanter.kuanterauto.entity.SiteInfo;

public class SitesAdapter extends BaseAdapter {
	LayoutInflater factory;
	List<SiteInfo> sites = new ArrayList<SiteInfo>();
	int currentPage = 1;
	int pageSize = 10;
	
	boolean localCity;

	public SitesAdapter(LayoutInflater factory,boolean localCity) {
		this.factory = factory;
		this.localCity = localCity;
	}

	@Override
	public int getCount() {
		int size;
		if(sites.size() > pageSize * currentPage) {
			size = pageSize * currentPage;
		} else {
			size = sites.size();
		}
		return size;
	}

	@Override
	public Object getItem(int i) {
		return sites.get(i);
	}

	@Override
	public long getItemId(int i) {
		return sites.get(i).getId();
	}

	@Override
	public View getView(int i, View view, ViewGroup viewgroup) {
		if(i < pageSize * currentPage) {
			if(view == null) {
				view = factory.inflate(R.layout.list_sites_item, null);
				
			}
			TextView site_name = (TextView) view.findViewById(R.id.site_name);
			TextView site_price = (TextView) view.findViewById(R.id.site_price);
			TextView site_distance = (TextView) view.findViewById(R.id.site_distance);
			TextView site_id = (TextView) view.findViewById(R.id.site_id);
//			ImageView site_star = (ImageView) view.findViewById(R.id.site_star);
			RatingBar rateBar = (RatingBar) view.findViewById(R.id.rate_bar);
			rateBar.setIsIndicator(true);
			float score = Float.valueOf(sites.get(i).getScore());
			rateBar.setRating(score);
			Log.i("aaa", score + "------------score");
			/*if(score <= 0) {
				site_star.setImageResource(R.drawable.point0);
			} else if(score > 0 && score <= 1) {
				site_star.setImageResource(R.drawable.point10);
			} else if(score > 1 && score <= 2) {
				site_star.setImageResource(R.drawable.point20);
			} else if(score > 2 && score <= 3) {
				site_star.setImageResource(R.drawable.point30);
			} else if(score > 3 && score <= 4) {
				site_star.setImageResource(R.drawable.point40);
			} else if(score > 4) {
				site_star.setImageResource(R.drawable.point50);
			}*/
			
			site_name.setText(sites.get(i).getSiteName());
			Log.i("bbb", sites.get(i).getMaxServicePrice() + "----");
			site_price.setText(sites.get(i).getMinServicePrice() + "~" + sites.get(i).getMaxServicePrice());
			if(!localCity) {
				Log.i("bbb", sites.get(i).getSiteDistance() + "    dist");
				double dis = getDoublePoint2(sites.get(i).getSiteDistance());
				if(dis > 50) {
					site_distance.setText(" >50KM");
				} else {
					site_distance.setText(dis + "KM");
				}
			} else {
				site_distance.setText(">50KM");
			}
			site_id.setText(sites.get(i).getId() + "");
		}
		return view;
	}
	
	public double getDoublePoint2(String distance) {
		double d_dis = Double.valueOf(distance);
		long ll = Math.round(d_dis * 100);
		d_dis= ll / 100.0;
		return d_dis;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getDatasSize() {
		return sites.size();
	}

	public List<SiteInfo> getSites() {
		return sites;
	}

	public void setSites(List<SiteInfo> sites) {
		this.sites = sites;
	}
	
	
	
}
